package com.neuedu.action.web;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.Food;
import com.neuedu.model.FoodMake;
import com.neuedu.model.FoodType;
import com.neuedu.model.UserMessage;
import com.neuedu.service.FoodService;
import com.neuedu.service.UserMessageService;
import com.neuedu.util.StringUtil;

@Controller
@RequestMapping(value = "/Personal")
public class PersonalAction {
	@Autowired
	private UserMessageService usermessageService;

	@Autowired
	private FoodService foodService;

	// *****************************************
	@RequestMapping(value = "/findUser")
	public String findById(UserMessage usermessage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		UserMessage um = usermessageService.findUserById(usermessage);
		request.setAttribute("userInfo", um);

		return "My_information";

	}

	// 文章
	@RequestMapping(value = "/findArticle")
	public String findArticle(Food food, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 美食作者
		List<Food> um = foodService.findFoodArticle(food);
		request.setAttribute("userArticle", um);

		return "My_article";

	}

	// 修改
	@RequestMapping(value = "updateUserMessage")
	public void updateUserMessage(MultipartFile file1, UserMessage usermessage, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// 文件的原始名称
		System.out.println("fileName：" + file1.getOriginalFilename());
		// 取得网站程序在服务器磁盘的路径
		String basePath = request.getServletContext().getRealPath("/");

		// 重新使用时间命名文件名，避免出现中文或不雅文件名
		String newFileName = System.currentTimeMillis()
				+ file1.getOriginalFilename().substring(file1.getOriginalFilename().lastIndexOf("."));

		String uploadPath = basePath + "/pages/userimg/" + newFileName;
		System.out.println("newFileName：" + uploadPath);

		try {
			File outputFile = new File(uploadPath);
			// 通过MultipartFile的方法直接写文件（注意这个时候）
			usermessage.setUserImg("userimg/" + newFileName);
			file1.transferTo(outputFile);
		} catch (IOException e) {
			System.out.println("上传文件出错！------");
		}

		int res = usermessageService.updateUserMessage(usermessage);
		Map<String, Object> map = new HashMap<String, Object>();
		if (res > 0) {
			/* map.put("success", true); */
			response.sendRedirect(request.getContextPath() + "/Personal/findUser.do");
		} else {
			map.put("success", false);
			map.put("errorMsg", "update user fail !!!");
		}
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

	// 菜谱详情页
	@RequestMapping(value = "/findFoodById")
	public String findFoodById(Food food, HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 所有菜谱
		Food f = foodService.findFoodById(food);
		request.setAttribute("fooddetail", f);
		return "Edit_article";

	}

}
